<?php
/**
 * 节点管理
 * 
 * @author  Lee<a605333742@gmail.com>
 * @time    2016-08-11
 */
namespace Admin\Controller;

class NodeController extends CommonController{
    /*
     * 构造函数
     * 
     * @return #
     */
    public function _initialize() {
        parent::_initialize();
        $this->db   =D("Node");
    }
    
    /*
     * index前置操作
     * 
     * @return #
     */
    public function _before_index(){
        $map['status']  =1;
        $map['level']   =0;
        $topNoList      =$this->db->where($map)->select();
        $this->assign("topNoList",$topNoList);
   }
   
   /*
    * 节点列表
    * 
    * @return #
    */
    public function index(){
        A("Common")->index('','',TRUE);
    }
   
   /*
    * show前值操作
    * 
    * @return #
    */
   public function _before_show(){
       $this->_before_index();
   }
   
   /*
     * ajax查询控制器名称并返回控制器
     * 
     * @return array $controlName
     */
    public function ajaxControl(){
        $ajaxP_id   =I("request.pid","","int");
        $controlName=$this->db->find($ajaxP_id);
        $controll   =$controlName['controller'];
        echo $controll;
    }
    
    /*
     * 新增节点操作
     * 
     * @return #
     */
    public function edit(){         
        if(!I("request.id")){
            $roleModel  =D("RoleNode");
            $roleModel->startTrans();
            $addNode    =A("Common")->insert();
            if($addNode['name']){
                $roleModel->rollback();
                $this->error($addNode['name']);
                exit;
            }
            $map['node_id'] =$this->db->where(array("status"=>1))->max('id');
            $map['role_id'] =2;
            $addRoleNode=$roleModel->add($map);
            if($addNode && $addRoleNode){
                $roleModel->commit();
                session('nodeList_s',null);
                session('nodeList_t',null);
                $this->success("节点新增成功,请刷新页面");
            }else{
                $roleModel->rollback();
                $this->error("节点新增失败");
            }
        }else{
            if(A("Common")->update()){
                session('nodeList_s',null);
                session('nodeList_t',null);
                $this->success("节点编辑成功，请刷新页面");
            }else{
                $this->error('节点编辑失败');
            }
        }
    }
    
    /*
     * 删除节点后置操作
     * 
     * @return #
     */
    public function _before_del(){ 
        session('nodeList_s',null);
        session('nodeList_t',null);
    }
}